package com.yexs.common.listener;

import com.yexs.common.file.Progress;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * https://www.cnblogs.com/zincredible/p/9060663.html
 *
 * @Description: java类作用描述
 * @Author: yexs
 * @CreateDate: 2018/5/29 18:16
 * @Version: 1.0
 */
@Component
public class FileUploadProgressListener implements ProgressListener {

    private HttpSession session;

    public void setSession(HttpSession session) {
        this.session = session;
        Progress status = new Progress();
        //保存上传状态
        session.setAttribute("status", status);
    }

    /**
     * 更新 上传进度实体的状态属性值
     *
     * @param bytesRead
     * @param contentLength
     * @param items
     */
    @Override
    public void update(long bytesRead, long contentLength, int items) {
        Progress status = (Progress) session.getAttribute("status");
        status.setBytesRead(bytesRead);
        status.setContentLength(contentLength);
        status.setItems(items);
    }

}